package com.xiaotu.common.util;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.xiaotu.common.util.Contour;
import com.xiaotu.common.util.Glyph;
import com.xiaotu.common.util.Point;

/**
 * @类名: XmlParser
 * @类功能描述: 解析xml
 * @时间 ：2011-4-13 上午08:33:50
 */
public class FontUtils{
	
	public static List<Glyph> buildGlyphList(Document document){
		
		if (document == null) {
			return null;
		}
		
		Element root = document.getRootElement();
		Element glyfElement = root.element("glyf");
		List<Element> glyphElementList = glyfElement.elements("TTGlyph");
		
		List<Glyph> glyphList = new ArrayList<Glyph>();
		for(Element glyphElement: glyphElementList) {
			//解析Element组装成glyph对象		
			
			int attributeCount =glyphElement.attributeCount();			
			if(attributeCount < 5 || "x".equals(glyphElement.attributeValue("name"))) {
			   continue;
			}	
			String id = glyphElement.attributeValue("id");
			String name = glyphElement.attributeValue("name");
			String xMin = glyphElement.attributeValue("xMin");
			String yMin = glyphElement.attributeValue("yMin");
			String xMax = glyphElement.attributeValue("xMax");
			String yMax = glyphElement.attributeValue("yMax");
			//System.out.println(id+"-->"+name+":"+xMin+"/"+yMin+"/"+xMax+"/"+yMax);
			Glyph glyph = new Glyph(id,name,xMin,yMin,xMax,yMax);
			
			List<Contour> contourList = new ArrayList<Contour>();
			List<Element> contourElementList = glyphElement.elements("contour");
			for(Element contourElement: contourElementList) {
				//System.out.println("===========contour===============");
				Contour contour = new Contour();
				List<Point> pointList = new ArrayList<Point>();
				List<Element> pointElementList = contourElement.elements("pt");
				for(Element pointElement: pointElementList) {
					String x = pointElement.attributeValue("x");
					String y = pointElement.attributeValue("y");
					String on = pointElement.attributeValue("on");					
					//System.out.println(x+"	"+y+"	"+on);
					Point point = new Point(x,y,on);
					pointList.add(point);
				}				
				contour.setPointList(pointList);
				contourList.add(contour);
			}
			glyph.setContourList(contourList);
			glyphList.add(glyph);			
		}
		
		return glyphList;
	}

	public static void main(String[] args) throws Exception
	{
		String transMessage = "<?xml version=\"1.0\" encoding=\"GBK\"?>"
				 + "<root>"
			     + "<ticketNotify>"
			     + "<ticket id=\"6000012007051000000231\" dealTime=\"20070510165423\" status=\"0000\" message=\"成功,系统处理正常\"/>"
			     + "<ticket id=\"6000012007051000000232\" dealTime=\"20070510165424\" status=\"2012\" message=\"禁止倍投\"/>"
			     + "</ticketNotify>" 
			     + "</root>";
		Document document = DocumentHelper.parseText(transMessage);		
		System.out.println("document="+document);
		System.out.println("root="+document.getRootElement().getName());
		
		//System.out.println(XMLUtils.getChild(document.getRootElement(),"ticket").getText());
	}
}